<?php
session_start();
$domain = $_GET['domain'];
mysql_connect("localhost", "", "");
mysql_query("USE stats_syslog");
$optionsquery = mysql_query("SELECT Recipient, COUNT(*) AS count FROM maillog WHERE Action = 'Allowed' AND Domain = '$domain' GROUP BY Recipient ORDER BY count DESC LIMIT 0,10");
while($row = mysql_fetch_row($optionsquery)) {
    $piece_names_temp[] = $row;
}
mysql_close();
if ($piece_names_temp) {
    foreach ($piece_names_temp as $key => $row) {
         $recipient_names[$key] = $row[0];
    }
    foreach ($piece_names_temp as $key => $row) {
         $recipient_values[$key] = $row[1];
    }
    $_SESSION['recipient_names_.$domain'] = $recipient_names;
    $_SESSION['recipient_values_.$domain'] = $recipient_values;
    echo "<html>";
    echo "<body>";
    echo "<b>Top 10 Recipients for Domain \"$domain\"</b><br><br>";
    echo "<table border=1>";
    echo "<tr>";
    echo "<td>Recipient</td><td>Number</td>";
    echo "</tr>";
    for ($x = 0; $x < count($recipient_names); $x++){
      echo "<tr><td>".$recipient_names[$x]."</td><td>".$recipient_values[$x]."</td></tr>";
      }
    echo "</table>";
    echo "<br><br><br>";
    echo "<img src=\"toprecipients-diagramm.php?domain\" border=\"0\">";
    echo "</body>";
    echo "</html>";
}
else {
    echo "<html>";
    echo "<body>";
    echo "No entries found!";
    echo "</body>";
    echo "</html>";
}
?>
